import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/order_model/get_pay_token_model.dart';
import 'package:yysk_app/model/order_model/order_incubation_detail_model.dart';
import 'package:yysk_app/model/order_model/order_detail_model.dart';
import 'package:yysk_app/net/api/order_api.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/confirm_payment/confirm_payment_page.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:oktoast/oktoast.dart';

class OrderDetailController extends GetxController {
  String id = Get.parameters['id'] ?? Get.arguments['id'] ?? '';
  String type = Get.parameters['type'] ?? Get.arguments['type'] ?? '';
  final buyOrderDetail = Rx<OrderDetailModel?>(null);
  final incubationOrderDetail = Rx<OrderIncubationDetailModel?>(null);
  @override
  void onInit() {
    super.onInit();
    // 初始化逻辑
    if (type == 'buy') {
      getBuyOrderDetail();
    } else {
      getIncubationOrderDetail();
    }
  }

  // 获取首发订单详情
  getBuyOrderDetail() {
    http.netFetch<OrderDetailModel>(
      '${OrderUrl.orderDetail}/$id',
      method: Method.get,
      success: (result) {
        buyOrderDetail.value = result.data;
      },
    );
  }

  // 获取孵化订单详情(购买-出售)
  getIncubationOrderDetail() {
    http.netFetch<OrderIncubationDetailModel>(
      '${OrderUrl.incubationOrderDetail}/$id',
      method: Method.get,
      success: (result) {
        incubationOrderDetail.value = result.data;
      },
    );
  }

  cancelOrder() {
    if (type == 'buy') {
      cancelBuyOrder(id);
    } else {
      cancelIncubationOrder(id);
    }
    Future.delayed(const Duration(milliseconds: 1000), () {
      // 初始化逻辑
      if (type == 'buy') {
        getBuyOrderDetail();
      } else {
        getIncubationOrderDetail();
      }
    });
  }

  cancelBuyOrder(id) {
    http.netFetch<dynamic>(
      OrderUrl.orderCancel,
      params: {'orderId': id},
      success: (result) {
        showToast('订单取消成功');
        getBuyOrderDetail();
        Navigator.of(Get.context!).pop();
      },
    );
  }

  cancelIncubationOrder(id) {
    http.netFetch<dynamic>(
      OrderUrl.incubationOrderCancel,
      params: {'orderId': id},
      success: (result) {
        showToast('订单取消成功');
        getIncubationOrderDetail();
        Navigator.of(Get.context!).pop();
      },
    );
  }

  //获取令牌
  Future<dynamic> getPayToken(dynamic sn, dynamic type) async {
    try {
      http.netFetch<GetPayTokenModel>(
        OrderUrl.getPayToken,
        method: Method.post,
        params: {"businessType": type, 'orderId': sn},
        success: (result) {
          Get.toNamed(
            '${ConfirmPaymentPage.path}?cashierToken=${result.data?.cashierToken}&accessToken=${result.data?.accessToken}&businessType=VIRTUAL_GOODS&clientType=web',
            arguments: {
              'cashierToken': result.data?.cashierToken,
              'accessToken': result.data?.accessToken,
              'businessType': 'DEBUT',
            },
          );

          // 在 Web 环境下调用 JavaScript
          // window.location.href =
          //     '${Env.envConfig.envPayUrl}?cashierToken=${result.data?.cashierToken}&accessToken=${result.data?.accessToken}&businessType=PHYSICAL_GOODS&clientType=web';
          return result;
        },
      );
    } catch (e) {
      consoleLog(e.toString());
    }
  }

  //获取孵化支付令牌
  Future<dynamic> getIncubationPayToken(dynamic sn, dynamic type) async {
    try {
      http.netFetch<GetPayTokenModel>(
        OrderUrl.getIncubationPayToken,
        method: Method.post,
        params: {"businessType": 'INCUBATE', 'orderIds': [sn]},
        success: (result) {
          Get.toNamed(
            '${ConfirmPaymentPage.path}?cashierToken=${result.data?.cashierToken}&accessToken=${result.data?.accessToken}&businessType=VIRTUAL_GOODS&clientType=web',
            arguments: {
              'cashierToken': result.data?.cashierToken,
              'accessToken': result.data?.accessToken,
              'businessType': 'DEBUT',
            },
          );
          // 在 Web 环境下调用 JavaScript
          // window.location.href =
          //     '${Env.envConfig.envPayUrl}?cashierToken=${result.data?.cashierToken}&accessToken=${result.data?.accessToken}&businessType=PHYSICAL_GOODS&clientType=web';
          return result;
        },
      );
    } catch (e) {
      consoleLog(e.toString());
    }
  }
}
